import os
import re
import time
import requests
import selenium
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait

current_time = time.strftime("%Y%m%d-%H%M%S", time.localtime())  # 当前系统时间

account_str = "account"
keywords_str = "passsword"
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
try:
    browser.get('https://stock.tuchong.com/')
except TimeoutException:
    print("Time Out")


def get_response(url):
    timeout = 10
    headers = {
        'Host': 'https://stock.tuchong.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
    }
    response = requests.get(url, headers=headers, timeout=timeout)
    return response


def new_fold():
    current_directory = os.getcwd()
    final_directory = os.path.join(current_directory, r'图虫购物车')
    if not os.path.exists(final_directory):
        os.makedirs(final_directory)


def prepare_work():
    new_fold()
    try:
        login_opt = browser.find_element_by_xpath(
            '//*[@id="root"]/div[1]/div/div[2]/div[3]/div/span[1]')  # 通过xpath找到"登录选项"位置
        login_opt.click()  # 点击登陆选项
        loginway_switch = browser.find_element_by_xpath(
            '/html/body/div[6]/div/div/div/div[2]/p[3]/span')  # 通过xpath找到"其他登陆方式"位置
        loginway_switch.click()  # 点击其他登陆方式
        login_bypswd = browser.find_element_by_xpath(
            '/html/body/div[6]/div/div/div/div[3]/div[1]/span[2]')  # 通过xpath找到"密码登陆"位置
        login_bypswd.click()  # 点击密码登录

        account_box = browser.find_element_by_xpath(
            '/html/body/div[6]/div/div/div/div[3]/form/div[1]/input')  # 通过xpath找到账号文本框位置
        account_box.send_keys(account_str)  # 输入账号信息

        keywords_box = browser.find_element_by_xpath(
            '/html/body/div[6]/div/div/div/div[3]/form/div[2]/input')  # 通过xpath找到密码文本框位置
        keywords_box.send_keys(keywords_str)  # 输入账号信息

        login_btn = browser.find_element_by_xpath('/html/body/div[6]/div/div/div/div[3]/button')  # 通过xpath找到"登陆按钮"位置
        login_btn.click()

    except:
        print('Login Failed')

    print('Login Success')
    time.sleep(5)

    try:
        shopping_cart = browser.find_element_by_xpath(
            '//*[@id="root"]/div[1]/div/div[2]/div[1]/a/i')  # 通过xpath找到"购物车"位置
        shopping_cart.click()  # 点击购物车

    except:
        print('Enter Shopping Cart Failed')
    print('Enter Success. Start Crawling......')


def page_switch():
    try:
        page_number = 1
        page_prefix = 'https://stock.tuchong.com/shoppingcart?page='
        page_number = str(page_number)
        page_suffix = '&size=100'
        browser.get(str(page_prefix + page_number + page_suffix))
        time.sleep(4)
        int(page_number)
    except TimeoutException:
        print("Time Out")


def reptile_work():
    for num in range(1, 100):
        xpath_prefix = '//*[@id="root"]/div[2]/div[2]/div['
        num_str = str(num)
        xpath_suffix = ']/a/div'
        full_xpath = xpath_prefix + num_str + xpath_suffix
        div_data = browser.find_element_by_xpath(full_xpath)
        print(div_data.get_attribute('data-lazy-url'))


    # //*[@id="root"]/div[2]/div[2]/div[1]/a/div
    # //*[@id="root"]/div[2]/div[2]/div[2]/a/div
    # //*[@id="root"]/div[2]/div[2]/div[3]/a/div
    # //*[@id="root"]/div[2]/div[2]/div[4]/a/div
    # //*[@id="root"]/div[2]/div[2]/div[5]/a/div



prepare_work()
page_switch()
reptile_work()